package top.deepdesigner.controller.promotion;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.deepdesigner.annotation.RequiresPermissionsDesc;
import top.deepdesigner.pojo.DtsAd;
import top.deepdesigner.service.DtsAdService;
import top.deepdesigner.util.ResponseUtil;
import top.deepdesigner.vo.CommonListVO;

import java.util.List;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/24 星期六 15:35:18 <br/>
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/ad")
public class AdController {

  @Autowired
  private DtsAdService adService;

  @RequiresPermissionsDesc(menu = {"推广管理", "广告管理"}, button = "获取列表")
  @RequiresPermissions(value = {"admin:ad:list"})
  @GetMapping("/list")
  public Object list(@RequestParam(defaultValue = "1") Integer page,
                     @RequestParam(defaultValue = "20") Integer limit,
                     @RequestParam(defaultValue = "add_time") String sort,
                     @RequestParam(defaultValue = "desc") String order,
                     @RequestParam(defaultValue = "") String name,
                     @RequestParam(defaultValue = "") String content) {
    CommonListVO<DtsAd> adListVO = new CommonListVO<>();
    //获取分页数量
    Long count = adService.getAdCount();
    List<DtsAd> dtsAds = adService.getAdList(page, limit, sort, order, name, content);
    adListVO.setTotal(count);
    adListVO.setItems(dtsAds);
    return ResponseUtil.ok(adListVO);
  }

  @RequiresPermissionsDesc(menu = {"推广管理", "广告管理"}, button = "添加")
  @RequiresPermissions(value = {"admin:ad:create"})
  @PostMapping("/create")
  public Object create(@RequestBody DtsAd ad) {
    DtsAd dtsAd = adService.createAd(ad);
    if (dtsAd == null) return ResponseUtil.fail(700, "广告名已存在");
    return ResponseUtil.ok(dtsAd);
  }

  @RequiresPermissionsDesc(menu = {"推广管理", "广告管理"}, button = "修改")
  @RequiresPermissions(value = {"admin:ad:update"})
  @PostMapping("/update")
  public Object update(@RequestBody DtsAd ad) {
    DtsAd dtsAd = adService.updateAd(ad);
    return ResponseUtil.ok(dtsAd);
  }

  @RequiresPermissionsDesc(menu = {"推广管理", "广告管理"}, button = "删除")
  @RequiresPermissions(value = {"admin:ad:delete"})
  @PostMapping("/delete")
  public Object delete(@RequestBody DtsAd ad) {
    adService.deleteAd(ad);
    return ResponseUtil.ok();
  }
}
